<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Message\Image;

class WechatController extends Controller
{
    
    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function server(){
        $wechat = app('wechat');
        $userApi = $wechat->user;
        
        $wechat->server->setMessageHandler(function($message) use($userApi){
//           return $message->FromUserName;// 用户的 openid
           //return $message->MsgType; // 消息类型：event, text....
            switch($message->MsgType){
                case 'event':
                    if($message->Event == 'subscribe'){ //关注
                        return '欢迎关注EAI交易圈';
                    }else if($message->EventKey == 'YOU_VOUTE_ME'){
                        return '谢谢点赞哦！';
                    }else{
                        return '你好';
                    }
                    break;
                case 'text':
                    return '你好'.$userApi->get($message->FromUserName)->nickname;
                    break;
                case 'image':
                    $image = new Image(['media_id'=>'U-IurB2c4Rn8XIdJu22FyLAPJUKIkerYocx_DAPnJRo']);
                    $wechat->staff->message($image)->to($message->FromUserName)->send();
                    return '回复一个图片给你';
                    break;
                case 'voice':
//                    $voice = new Voice(['media_id'=>'U-IurB2c4Rn8XIdJu22FyI-CoQnRzUciNa17iojB3ng']);
//                    $wechat->staff->message($voice)->to($message->FromUserName)->send();
                    return '别发语音了，唱首歌吧';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                // ... 其它消息
                default:
                    return 'Hello，欢迎来到EAI交易圈';
                    break;
            }    
        });
        
        return $wechat->server->serve();
    }
    
    public function user(){
        $user = session('wechat.oauth_user'); // 拿到授权用户资料
        dd($user);
    }
    

}
